/**
 * @author:linjiye
 * @date: 2024/12/25
 * @desc:
 **/

import {StyleSheet, Text, TouchableOpacity, View} from 'react-native'
import React, {useEffect, useState} from 'react'
import TabCard from "@/components/TabCard";
import PageFormBtn from "@/components/PageFormBtn";
import SdsrPage from "@/components/aiOriginality/SdsrPage";
import AiInnovationWrap from "@/components/aiInnovation/AiInnovationWrap"
import PageScrollView from "@/components/PageScrollView";
import {useRouter} from "expo-router";
import FcButton from "@/components/FcButton";
import {useSelector} from "react-redux";
import {useMmContentManagePubAddMutation} from "@/store/api/aiOriginality/aiOriginalityApi";
import {Toast} from "@ant-design/react-native";

type Props = {}

const result = (props: Props) => {
    const [disable, setDisable] = useState(false)
    const [vData,setVData] = useState({})
    const [isAdd,setIsAdd] = useState(false)
    const router = useRouter();
    const p = useSelector(state => state.aiInnovation.params)
    const videoData = useSelector(state => state.keywordExplosion.videoData)

    const [addFun] = useMmContentManagePubAddMutation()

    useEffect(() => {
        if(p) {
            let disable = false
            if(p.remember){
                disable =!(p.groupId  &&  p.profitModelId && p.text)

            } else {
                disable = !p.text
            }
            setDisable(disable)
        }
    }, [p]);

    useEffect(() => {
        if(videoData) {
            console.log(videoData);
            setVData(videoData)
        }
    }, [videoData]);

    function handleAdd() {
        Toast.show({
            icon: 'loading',
            content: '正在导入中...',
            duration: 0,
            mask: false,
        })
        let params = {
            content: p?.text,
            templateId: "1749679210634153986",
            title: null
        }
        addFun(params).then(({data: {code, result}}) => {
            console.log(code, result);
            if (code === 200) {
                setIsAdd(true)
                Toast.show({
                    content: result,
                    duration: 3,
                    mask: false,
                })
            }
        }).finally(() => {
            Toast.removeAll()
        })
    }

    return (
        <PageScrollView
            isHaveHeader
            isTransparentTab
            pageBottom = {
                <View>
                    <View style={styles.bottom_btn}>
                        <View style={{flex: 1, marginRight: 4}}>
                            <FcButton
                                disable={disable}
                                onClick={() => handleAdd()}
                                type='default'
                                style={{
                                    width: '100%',
                                    height: 40,
                                    justifyContent: 'center',
                                    backgroundColor: '#F1F2F4',
                                    borderRadius: 8
                                }}
                                itemStyle={{fontSize: 15}}
                                textName='导入内容管理'/>
                        </View>
                        <View style={{flex: 1, marginLeft: 4}}>
                            <FcButton
                                disable={disable}
                                onClick={() => router.push('/aiOriginality/result')}
                                type='BjJB'
                                colors={['#6989FF', '#C65EFD']}
                                style={{width: '100%', height: 40, justifyContent: 'center'}}
                                itemStyle={{borderRadius: 8, fontSize: 15}}
                                textName='生成文案'/>
                        </View>
                    </View>
                </View>
            }
        >
            <AiInnovationWrap vData={vData}/>
        </PageScrollView>
    )
}

export default result

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
    },
    bottom_btn: {
        flexDirection: "row",
        alignItems: "center",
        paddingHorizontal: 16,
        height: 56,
        justifyContent: "space-between",
        backgroundColor: 'white'
    },

    btn_wrap: {
        flexDirection: "row",
        alignItems: "center",
        padding: 12,
        backgroundColor: 'white',
        marginBottom: 8,
        borderTopWidth: 0.5,
        borderTopColor: 'rgba(0, 0, 0, 0.1)',
    },
    btn: {
        flexDirection: "row",
        alignItems: "center",
        backgroundColor: '#F1F2F4',
        borderRadius: 8,
        paddingHorizontal: 8,
        height: 28,
        marginRight: 8
    },
})